﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace linq_to_dataset
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           if (button1.Text=="insert")
           {
               groupBox1.Visible = true;
               button1.Text = "ok";
              
               
           }
            if (button1.Text=="ok")
            {
               try
               {
                   DataClasses1DataContext context = new DataClasses1DataContext();
                   // var sv = from p in context.sinhviens  select p;
                   sinhvien sv = new sinhvien();
                   sv.hoten = txthoten.Text;
                   sv.id = int.Parse(txtid.Text.ToString());
                   context.sinhviens.InsertOnSubmit(sv);
                   context.SubmitChanges();
               }
               catch (System.Exception ex)
               {
               	
               }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            DataClasses1DataContext context = new DataClasses1DataContext();
            
            IEnumerable<sinhvien> sv = from p in context.sinhviens select p;
            foreach (sinhvien s in sv)
            {
                listBox1.Items.Add(s.hoten.ToString());
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(listBox1.SelectedValue.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
        }
    }
}
